package com.ffyc.cemp.services.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.cemp.dao.mapper.IMovieMapper;
import com.ffyc.cemp.dao.mapper.IRoleMapper;
import com.ffyc.cemp.dao.mapper.IUserMapper;
import com.ffyc.cemp.domain.entity.Movie;
import com.ffyc.cemp.domain.entity.Role;
import com.ffyc.cemp.domain.entity.User;

import com.ffyc.cemp.domain.vo.PageVo;
import com.ffyc.cemp.services.service.IMovieService;

import com.ffyc.cemp.tools.WordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MovieServiceImpl implements IMovieService {
    @Autowired
    private IMovieMapper movieMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate; //存储倒排索引

    @Override
    public PageVo<List<Movie>> findAll(int curr, int pageSize) {

        IPage<Movie> page = new Page<>(curr, pageSize);
        LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();

        IPage<Movie> p = movieMapper.selectPage(page, queryWrapper);
        PageVo<List<Movie>> pageVo = new PageVo<>(
                p.getSize(),
                p.getTotal(),
                p.getCurrent(),
                p.getRecords());


        return pageVo;
    }

    @Override
    public List<String> findAllTypes() {

        List<Movie> movies = movieMapper.selectList(null);

        return movies.stream()
                .map(Movie::getType)
                .distinct()
                .collect(Collectors.toList());

    }

    @Override
    public void addMovie(Movie movie) {
        movieMapper.insert(movie);
    }

    @Override
    public void wordFromDb() {

        List<Movie> movies = movieMapper.selectList(null);

        for (Movie movie : movies) {
            String name = movie.getName();
            List<String> worlds = WordsUtil.toWorlds(name); //流浪  地球   id=1

            for (String world : worlds) { //电影名称列
                stringRedisTemplate.opsForSet().add(world, String.valueOf(movie.getId()));  //流浪-1     地球-1
            }

            String direcor = movie.getDirector();
            worlds = WordsUtil.toWorlds(direcor);
            for (String world : worlds) {  //导演列
                stringRedisTemplate.opsForSet().add(world, String.valueOf(movie.getId()));
            }

            String stars = movie.getStars();
            worlds = WordsUtil.toWorlds(stars);

            for (String world : worlds) {//演员列
                stringRedisTemplate.opsForSet().add(world, String.valueOf(movie.getId()));
            }

        }


    }

    @Override
    public List<Movie> findByKeywords(String keywords) {
        Set<String> members = stringRedisTemplate.opsForSet().members(keywords);

        LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(Movie::getId, members);

        return movieMapper.selectList(queryWrapper);


    }

    @Override
    public List<Movie> findByDateCurrent(int pageSize) {
        LambdaQueryWrapper<Movie> queryWrapper
                = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Movie::getReleaseDate);

        IPage<Movie> page = new Page<>(1, pageSize);

        IPage<Movie> p = movieMapper.selectPage(page, queryWrapper);

        return p.getRecords();
    }

    @Override
    public List<Movie> hotMovie(int num) {
        LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Movie::getReleaseDate);
        IPage<Movie> page = new Page<>(1, num);
        IPage<Movie> p = movieMapper.selectPage(page, queryWrapper);
        List<Movie> records = p.getRecords();
        records.sort((o1, o2) -> o2.getRate() - o1.getRate());

        return records;

    }

    @Override
    public Movie findById(int movieId) {
        return movieMapper.selectById(movieId);
    }


}
